/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package candidato;

import Conexao.Conexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VENANCIO
 */
public class CandidatoDao implements CandidatoInt{
    
    private Connection con;
    private String sql;
    
    private CandidatoBean candidato;
    private List<CandidatoBean>  listaCandidato;
   
    public CandidatoDao(){
        
    }
    
    /**
     * Método que insere um novo candidato à tabela
     * @param c 
     */
    public void inserir(CandidatoBean c) {
        try {
            
            con = new Conexao().getConexao();
            sql = "INSERT INTO CANDIDATO(CANDIDATO_CPF, CANDIDATO_SENHA, CANDIDATO_NOME, CANDIDATO_EMAIL) "
                    + "VALUES(?,?,?,?)";
     
            int cont=1;
            PreparedStatement stmt = con.prepareStatement(sql);
            stmt.setString(cont++, c.getCpf());
            stmt.setString(cont++, c.getSenha());
            stmt.setString(cont++, c.getNome());
            stmt.setString(cont++, c.getEmail());
            stmt.executeUpdate();
            stmt.close();
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CandidatoDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CandidatoDao.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(CandidatoDao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    /**
     * Consulta todos os candidatos 
     * @return uma lista de candidatos
     */
    @Override
    public List<CandidatoBean> consulta(){
        List<CandidatoBean> candidato = new ArrayList<CandidatoBean>();
        try {
            con = new Conexao().getConexao();
            sql = "SELECT * FROM CANDIDATO";
            
            PreparedStatement stmt = con.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            
            while(rs.next()){
                CandidatoBean c = new CandidatoBean();
                c.setCpf(rs.getString("CANDIDATO_CPF"));
                c.setNome(rs.getString("CANDIDATO_NOME"));
                c.setSenha(rs.getString("CANDIDATO_SENHA"));
                c.setEmail(rs.getString("CANDIDATO_EMAIL"));
                candidato.add(c);
            }
            
            rs.close();
            stmt.close();
            return candidato;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CandidatoDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CandidatoDao.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(CandidatoDao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }   
        return candidato;
    }
    
}
